﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TTEngine.Graphics;
using TTEngine.Impl.Desktop;

namespace voidbox.Code
{
    public class VBAppConfig
    {
        public string Title
        {
            get;
            private set;
        } = "";
        public int PixelWidth
        {
            get;
            private set;
        }
        public int PixelHeight
        {
            get;
            private set;
        }

        public bool Debug
        {
            get;
            private set;
        }
        public int DebugPort
        {
            get;
            private set;
        } = 0;

        List<string> _LoadJs;
        public string[] LoadJs
        {
            get
            {
                return _LoadJs.ToArray();
            }

        }
        public void Init(string text)
        {
            var lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string curGroup = "";
            foreach (var line in lines)
            {
                if (line.Length == 0)
                    continue;
                if (line[0] == '#')
                    continue;
                if (line[0] == '[')
                    curGroup = line.Substring(1, line.IndexOf(']') - 1);
                else
                    ParseLine(curGroup, line);

            }
        }
        void ParseLine(string curGroup, string line)
        {
            var infos = line.Split(new char[] { ' ', '=', ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (infos.Length > 1)
            {
                var key = infos[0];
                if (curGroup == "StartOption")
                {
                    if (key == "Title")
                    {
                        Title = infos[1];
                    }
                    if (key == "PixelWidth")
                    {
                        PixelWidth = int.Parse(infos[1]);
                    }
                    if (key == "PixelHeight")
                    {
                        PixelHeight = int.Parse(infos[1]);
                    }

                    if (key == "LoadJs")
                    {
                        _LoadJs = new List<string>();
                        for (var i = 1; i < infos.Length; i++)
                        {
                            _LoadJs.Add(infos[i]);
                        }
                    }
                    if (key == "Debug")
                    {
                        var v = infos[1].ToLower();
                        Debug = (v == "true" || v == "1");
                    }
                    if (key == "DebugPort")
                    {
                        DebugPort =int.Parse(infos[1]);
                    }
                }

            }
        }
    }
}
